package com.workstudy.wstudy.controller;

import java.util.List;

import com.workstudy.common.core.domain.entity.SysUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.workstudy.common.annotation.Log;
import com.workstudy.common.enums.BusinessType;
import com.workstudy.wstudy.domain.WstMessage;
import com.workstudy.wstudy.service.IWstMessageService;
import com.workstudy.common.core.controller.BaseController;
import com.workstudy.common.core.domain.AjaxResult;
import com.workstudy.common.utils.poi.ExcelUtil;
import com.workstudy.common.core.page.TableDataInfo;

/**
 * 消息管理Controller
 * 
 * @author sun
 * @date 2021-04-04
 */
@Controller
@RequestMapping("/wstudy/message")
public class WstMessageController extends BaseController
{
    private String prefix = "wstudy/message";

    @Autowired
    private IWstMessageService wstMessageService;

    @GetMapping()
    public String message(ModelMap map)
    {
        SysUser loginUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        map.put("receiveUserId",loginUser.getUserId());
        return prefix + "/message";
    }

    /**
     * 查询消息管理列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WstMessage wstMessage)
    {
        startPage();
        List<WstMessage> list = wstMessageService.selectWstMessageList(wstMessage);
        return getDataTable(list);
    }

    /**
     * 导出消息管理列表
     */
    @RequiresPermissions("wstudy:message:export")
    @Log(title = "消息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WstMessage wstMessage)
    {
        List<WstMessage> list = wstMessageService.selectWstMessageList(wstMessage);
        ExcelUtil<WstMessage> util = new ExcelUtil<WstMessage>(WstMessage.class);
        return util.exportExcel(list, "message");
    }

    /**
     * 新增消息管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存消息管理
     */
    @Log(title = "消息管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WstMessage wstMessage)
    {
        return toAjax(wstMessageService.insertWstMessage(wstMessage));
    }

    /**
     * 修改消息管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        WstMessage wstMessage = wstMessageService.selectWstMessageById(id);
        mmap.put("wstMessage", wstMessage);
        return prefix + "/edit";
    }

    /**
     * 修改保存消息管理
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WstMessage wstMessage)
    {
        return toAjax(wstMessageService.updateWstMessage(wstMessage));
    }

    /**
     * 修改保存消息管理
     */
    @PostMapping("/setRead")
    @ResponseBody
    public AjaxResult setRead(WstMessage wstMessage)
    {
        return toAjax(wstMessageService.setRead(wstMessage));
    }

    /**
     * 删除消息管理
     */
    @Log(title = "消息管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(wstMessageService.deleteWstMessageByIds(ids));
    }

    @PostMapping( "/send")
    @ResponseBody
    public AjaxResult send(WstMessage message)
    {
        return toAjax(wstMessageService.sendMessage(message));
    }
}
